package com.jch.string_decoder;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.FixedLengthFrameDecoder;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;

import java.nio.charset.Charset;

public class DemoChannelInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        // 字符串解码器

        // 基于换行符 "\r\n"
        channel.pipeline().addLast(new LineBasedFrameDecoder(1024));

        // 基于指定字符串 【若指定换行符，则等同于LineBasedFrameDecoder】
        //channel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,false, Delimiters.lineDelimiter()));

        // 基于最大长度
        //channel.pipeline().addLast(new FixedLengthFrameDecoder(4));

        // 解码转String，注意编码格式
        channel.pipeline().addLast(new StringDecoder(Charset.forName("UTF-8")));

        // 自定义 数据处理器
        channel.pipeline().addLast(new DemoServerHandler());
        channel.pipeline().addLast(new SendAndReadHandler());
    }
}
